const { dbQuery } = require("../common/db");
const { extractErrorInfo, uuid } = require("../common/utils");

module.exports = {
    loadTaskList: async function (params) {
        var result = await dbQuery("select * from spider_task order by create_datetime asc");
        return result.success ? result.data : [];
    },
    loadTaskSpiderRecords: async function (params) {
        var result = { success: false };
        try {
            var countResult = null;
            if (params.task_id) {
                countResult = await dbQuery("select count(1) as count from spider_record where task_id = ? ", [params.task_id]);
            } else {
                countResult = await dbQuery("select count(1) as count from spider_record");
            }
            if (countResult.success) {
                // console.log("countResult", countResult);
                if (params.task_id) {
                    result = await dbQuery("select sr.spider_id, sr.spider_title, sr.task_id, sr.spider_url, sr.spider_status, sr.spider_datetime, st.task_name from spider_record sr left join spider_task st on sr.task_id = st.task_id where sr.task_id = ? order by spider_datetime desc limit ?,?", [params.task_id, params.start, params.limit]);
                } else {
                    result = await dbQuery("select sr.spider_id, sr.spider_title, sr.task_id, sr.spider_url, sr.spider_status, sr.spider_datetime, st.task_name from spider_record sr left join spider_task st on sr.task_id = st.task_id  order by sr.spider_datetime desc limit ?,?", [params.start, params.limit]);
                }
                if (result.success) {
                    result.total = countResult.data[0].count;
                }
            }
            return result;
        } catch (e) {
            result.error = extractErrorInfo(e);
            console.log(e)
        }
        return result;
    },
    getTaskConfig: async function (taskId) {
        var result = { success: false };
        try {
            result = await dbQuery("select * from spider_task_config where task_id = ?", [taskId]);
        } catch (e) {
            result.error = extractErrorInfo(e);
            console.log(e)
        }
        return result;
    },
    saveTaskInfo: async function (data) {
        var result = { success: false };
        try {
            if (data.task_id) {
                result = await dbQuery("update spider_task set task_name = ?, root_url = ? where task_id = ?", [data.task_name, data.root_url, data.task_id])
            } else {
                data.task_id = uuid();
                result = await dbQuery("insert into spider_task(task_id, task_name, root_url, create_datetime, task_status) values(?,?,?,NOW(),0)", [data.task_id, data.task_name, data.root_url]);
            }
            if (result.success) {
                var res = await dbQuery("delete from spider_task_config where task_id = ?", [data.task_id]);
                if (res.success) {
                    result = await dbQuery("insert into spider_task_config(task_id, spider_root_url_script, spider_content_url_script, spider_content_script, spider_timeout,spider_crons) values(?,?,?,?,?,?)", [data.task_id, data.spider_root_url_script, data.spider_content_url_script, data.spider_content_script, data.spider_timeout, data.spider_crons]);
                    result.data = data;
                } else {
                    result = res;
                }
            }
        } catch (e) {
            result.error = extractErrorInfo(e);
            console.log(e)
        }
        return result;
    },
    deleteTaskInfo: async function (tasId) {
        var result = { success: false };
        try {
            result = await dbQuery("delete from spider_task_config where task_id = ?", [tasId]);
            if (result.success) {
                result = await dbQuery("delete from spider_task where task_id = ?", [tasId]);
            }
        } catch (e) {
            result.error = extractErrorInfo(e);
            console.log(e)
        }
        return result;
    },
    getSpiderContent: async function (spiderId) {
        var result = { success: false };
        try {
            result = await dbQuery("select spider_content from spider_record where spider_id = ?", [spiderId]);
            if (result.success && result.data && result.data.length > 0) {
                result.data = result.data[0].spider_content;
            }
        } catch (e) {
            result.error = extractErrorInfo(e);
            console.log(e)
        }
        return result;
    }
}